//
//  ViewController.m
//  WexinLogin
//
//  Created by 朱兴隆 on 16/4/8.
//  Copyright © 2016年 朱兴隆. All rights reserved.
//

#import "ViewController.h"
#import "WXApi.h"
#import "AppDelegate.h"

@interface ViewController ()<WXLoginDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;

@end

@implementation ViewController
// 微信登录方法
- (IBAction)didClickLoginButton:(id)sender {
    // 判断微信客户端
    if ([WXApi isWXAppInstalled]) {
        [self sendAuthRequest];
    }
    
}
// 请求微信登录
- (void)sendAuthRequest {
    SendAuthReq *request = [[SendAuthReq alloc] init];
    // 请求微信登录授权权限
    request.scope = @"snsapi_userinfo";
    // 应用申请微信登录标识
    request.state = @"蓝鸥3G";
    // 发送
    [WXApi sendReq:request];
    
}

// 展示用户数据
- (void)wxLoginReceiveDict:(NSDictionary *)userInfo {
    NSString *imageUrlStr = userInfo[@"headimgurl"];
    NSString *userName = userInfo[@"nickname"];
    // 获取图片data
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlStr]];
    UIImage *image = [UIImage imageWithData:imgData];
    // 展示
    dispatch_async(dispatch_get_main_queue(), ^{
        self.headImageView.image = image;
        self.userNameLabel.text = userName;
    });
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
